package com.itheima.ydd.controller;


import com.itheima.ydd.config.R;
import com.itheima.ydd.dto.PageDto;
import com.itheima.ydd.dto.SysUserDto;
import com.itheima.ydd.service.ISysConfigService;
import com.itheima.ydd.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController

@Slf4j
@CrossOrigin
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysConfigService sysConfigService;

/*
    @PostMapping
    @RequestMapping("/index/login")
    public R login(@RequestBody SysUser sysUser){
        log.info("登陆用户：{}",sysUser.toString());

        String loginName = sysUser.getLoginName();
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getLoginName, loginName);
        SysUser one = sysUserService.getOne(wrapper);

        if (one == null){
            return R.error("账号不存在");
        }else if (one.getStatus() == 0){
            return R.error("账号已禁用");
        }

        String password = sysUser.getPassword();
        if (one.getPassword().equals(generatePassword(password))){
            //把用户id 加密进token里

            // 生成token信息
            Map retMap = new HashMap();
            Map tokenMap = new HashMap<>();
     */
/*       String roleId = one.getRoleId()
                    .replace("[","")
                    .replace("]", "")
                    .replace("\"", "");*//*

            List<String> roleIds= one.getRoleId();

            tokenMap.put("userId", one.getId());
            for (String roleId : roleIds) {
                tokenMap.put("roleId", roleId);
            }

            String token = JwtUtils.getToken(tokenMap);
            retMap.put("token", token);
            return R.success(retMap);
        }
        return R.error("密码错误");

    }



    @GetMapping
    @RequestMapping("/index/getConfig")
    public R <Map>systemConfig(){
        LambdaQueryWrapper<SysConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysConfig::getTabValue,"system");
        List<SysConfig> sysConfigs = sysConfigService.list(wrapper);

        HashMap<String, String> map = new HashMap<>();
        for (SysConfig sysConfig : sysConfigs) {
            map.put(sysConfig.getAttrKey(),sysConfig.getAttrValue());
        }

        return R.success(map);

    }

    @PostMapping
    @RequestMapping("/index/profile")
    public R profile(HttpServletRequest request){
        String token = request.getHeader("x-token");

        Claims claims = JwtUtils.getClaims(token);
        Long userId = Long.valueOf((String) claims.get("userId"));

        log.info("用户id:{}",userId);
        return sysUserService.profile(userId);
    }

    */
/** md5加密
     * @param password 密码
     *//*

    private String generatePassword(String password){
        return DigestUtils.md5DigestAsHex(password.getBytes());
    }
*/


    /**
     * 分页
     * @param loginName
     * @param status
     * @param phone
     * @param page
     * @param limit
     * @return
     */
    @GetMapping
    @RequestMapping("/sys_user/list")
    public R<PageDto> list(@RequestParam("login_name") String loginName, String status, String phone, int page , int limit){
      PageDto pageDto= sysUserService.getList(loginName,status,phone,page,limit);

             return R.success(pageDto);
    }


    @PostMapping
    @RequestMapping("/sys_user/add")
    public R<String> add(@RequestBody SysUserDto sysUserDto  ){

        log.info("sysdto",sysUserDto);
        sysUserDto.setCreateDate(LocalDateTime.now());
        sysUserService.save(sysUserDto);

        return R.success("新增成功");
    }

    /**
     * 修改回显
     * @param id
     * @return
     */
    @GetMapping
    @RequestMapping("/sys_user/rowInfo")
    public R<SysUserDto>rowInfo(String id){
        log.info("id"+id);

        SysUserDto sysUserDto=sysUserService.getByIdWhisRole(id);

        return R.success(sysUserDto);
    }


    @GetMapping
    @RequestMapping("/sys_user/del")
    public R<String>del(@RequestParam String id){

        sysUserService.detete(id);

        return R.success("删除成功");

    }

    @PostMapping
    @RequestMapping("/sys_user/update")
    public R<String>update(@RequestBody SysUserDto sysUserDto){
        sysUserService.updateById(sysUserDto);
        return R.success("修改成功");
    }



}
